package com.text.controller;

import com.github.pagehelper.PageInfo;
import com.text.entity.Category;
import com.text.entity.Goods;
import com.text.service.CategoryService;
import com.text.service.GoodsService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
@Controller
@RequestMapping("/admin")
public class GoodsCtrl {

    @Resource
    GoodsService goodsService;

    @Resource
    CategoryService categoryService;



//    @RequestMapping("/goodsList")
//    public String getGoodsList(Model model){
//        ArrayList<Goods> goodsList = goodsService.getGoodsList();
//        model.addAttribute("goodsList",goodsList);
//        return "goodsList";
//    }

    @RequestMapping(value = "goodsList",method = {RequestMethod.GET,RequestMethod.POST})
    public String getGoodsListPage(@RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "pageSize",required = false,defaultValue = "6") Integer pageSize,
                                   Model model){
        PageInfo<Goods> goodsListPage = goodsService.queryAllByPage(pageNum,pageSize);
        model.addAttribute("goodsListPage",goodsListPage);
        return "goodsList";
    }

    @GetMapping("/delGoods/{id}")
    public String delGoods(@PathVariable("id") int id, Model model) {
        int flag =  goodsService.deleteById(id);
        if (flag > 0){
            model.addAttribute("msg","删除成功！");
        } else{
            model.addAttribute("msg","该商品不允许删除！");
        }
        return "forward:/admin/goodsList";
    }

    @ResponseBody
    @GetMapping("/delGoods2/{id}")
    public int delGoods2(@PathVariable("id") int id,Model model){
        int flag=goodsService.deleteById(id);
        return flag;
    }

    @GetMapping("/gotoAddGoods")
    public String gotoAdd(Model model){
        ArrayList<Category> categoryList = categoryService.queryAll();
        model.addAttribute("categoryList",categoryList);
        return "goodsAdd";
    }

    @PostMapping("/addGoods")
    public String addGoods(Goods goods,Model model){
        int num = goodsService.insert(goods);
        if (num > 0){
            model.addAttribute("msg",goods.getName()+"添加成功！");
        } else{
            model.addAttribute("msg","添加失败！");
        }
        return "forward:/admin/goodsList";
    }
    @GetMapping("goods/{id}")
    public String hyqgoods(@PathVariable("id") int id,Model model){
        Goods edit = goodsService.hyqqueryById(id);
        model.addAttribute("edit",edit);
        ArrayList<Category> categoryList = categoryService.queryAll();
        model.addAttribute("categoryList",categoryList);
        return "goodsEdit";
    }

    @PostMapping("/hyqeditGoods")
    public String hyqeditGoods(Goods goods,Model model){
        System.out.println(goods);
        int num= goodsService.hyqupdate(goods);
        if(num > 0){
            model.addAttribute("msg","商品【"+goods.getName()+"】更新成功！");
        }else{
            model.addAttribute("msg","商品【"+goods.getName()+"】更新失败！");
        }
        return "forward:/admin/goodsList";
    }
}

